﻿using System;
using System.Linq;
using System.Web.Mvc;

using Flagwind.ComponentModel.DataAnnotations;

namespace Flagwind.Web.Plugins
{
	public class PluginControllerActionInvoker : ControllerActionInvoker
	{
		#region 重写方法

		protected override ActionDescriptor FindAction(ControllerContext controllerContext, ControllerDescriptor controllerDescriptor, string actionName)
		{
			var actionDescriptor = base.FindAction(controllerContext, controllerDescriptor, actionName);

			if(actionDescriptor != null)
			{
				// 获取验证规则申明 Attribute
				var validationRuleAttribute = actionDescriptor.GetCustomAttributes(true).OfType<ValidationRulesAttribute>().FirstOrDefault();

				controllerContext.RouteData.DataTokens.Add("validationRules", validationRuleAttribute != null ? validationRuleAttribute.Rules : null);
			}

			return actionDescriptor;
		}

		#endregion
	}
}
